<?php

namespace app\api\controller\v1;

use app\admin\model\BdOrg;
use think\Cache;
use think\Env;

class Payrecord extends Base
{

    public function listPayrecord()
    {
        $map        = [];

        $fk_bdl_paystatus   = !empty($this->params['fk_bdl_paystatus']) ? $this->params['fk_bdl_paystatus'] : [];
        $fk_bdl_amount      = !empty($this->params['fk_bdl_amount']) ? $this->params['fk_bdl_amount'] : [];

        $fk_bdl_erppaytime  = !empty($this->params['fk_bdl_erppaytime']) ? $this->params['fk_bdl_erppaytime'] : [];
        $page               = $this->params['page'] ?? 1;


        $cacheTag       = \config('cache_tag')['v1_payrecord_listPayrecord'];

        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . $page . json_encode($fk_bdl_paystatus) . json_encode($fk_bdl_amount) . json_encode($fk_bdl_erppaytime);
        $data           =  Cache::get($cacheKey);
        //组装条件
        if(!empty($fk_bdl_paystatus))
        {
            $map['fk_bdl_paystatus'] = $fk_bdl_paystatus;
        }
        if(!empty($fk_bdl_erppaytime))
        {
            $fk_bdl_erppaytime = explode('~',$fk_bdl_erppaytime);

            $map['fk_bdl_erppaytime'] = ['between time',[$fk_bdl_erppaytime[0] . "00:00:00",$fk_bdl_erppaytime[1] . "23:59:59"]];
        }


        if(!$data)
        {
            //组装条件
            $data = \app\admin\model\Payrecord::
            whereIn('forgid',$this->forgids)
                ->where('fk_bdl_bd_datacenter',$this->datacenter)
                ->where('fk_bdl_bd_customer',$this->customer_id)
                ->page($page,$this->pageSize)
                ->order('id','desc')
                ->where($map);


            if(!empty($fk_bdl_amount))
            {
                $fk_bdl_amount = explode('~',$fk_bdl_amount);
                $data->whereBetween('fk_bdl_amount',[$fk_bdl_amount[0],$fk_bdl_amount[1]]);
            }
            $data = $data->paginate($this->pageSize, false, ['page' => $page]);

            $count = [
                'daishenhe' => \app\admin\model\Payrecord::whereIn('forgid',$this->forgids)->where('fk_bdl_bd_datacenter',$this->datacenter)->where('fk_bdl_bd_customer',$this->customer_id)->where('fk_bdl_paystatus',1)->count(),
                'weitongguo' => \app\admin\model\Payrecord::whereIn('forgid',$this->forgids)->where('fk_bdl_bd_datacenter',$this->datacenter)->where('fk_bdl_bd_customer',$this->customer_id)->where('fk_bdl_paystatus',2)->count(),
                'yiqueren' => \app\admin\model\Payrecord::whereIn('forgid',$this->forgids)->where('fk_bdl_bd_datacenter',$this->datacenter)->where('fk_bdl_bd_customer',$this->customer_id)->where('fk_bdl_paystatus',3)->count(),
            ];
            $data = [
                'data'  => $data,
                'count' => $count,
            ];
            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);
        }



        return $this->success('',$data);

    }

    public function addPayrecord()
    {
        $check      = $this->plulicValidate($this->params,
            ['fk_bdl_amount|付款金额','fk_bdl_payvoucher|付款凭证','fk_bdl_paytype|付款付款类型'],
            ['require','require','require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        $forg       = BdOrg::get($this->forgid);
        $orderNo    = makeMiNiOrderNo('WXFK',$forg['fnumber']);

        $data        = [
            'fk_bdl_bd_customer'    => $this->customer_id,
            'fk_bdl_payvoucher'     => $this->params['fk_bdl_payvoucher'],
            'fk_bdl_amount'         => $this->params['fk_bdl_amount'],
            'fk_bdl_paytime'        => date('Y-m-d H:i:s',time()),
            'fk_bdl_paystatus'      => 1,
            'fk_bdl_wxbillno'       => $orderNo,
            'fk_bdl_bd_datacenter'  => $this->datacenter,
            'forgid'                => $this->forgid,
            'fk_bdl_userid'         =>$this->user_id,
            'fk_bdl_paytype'        => $this->params['fk_bdl_paytype']
        ];
        $res         = \app\admin\model\Payrecord::create($data);

        if($res)
        {
            $cacheTag       = \config('cache_tag')['v1_payrecord_listPayrecord'];
            Cache::clear($cacheTag);

            $this->success('上传成功,请等待审核');
        }

        $this->error('上传失败');
    }

    public function editPayrecord()
    {
        $check      = $this->plulicValidate($this->params,
            ['fk_bdl_amount|付款金额','fk_bdl_payvoucher|付款凭证','id|ID','fk_bdl_paytype|付款付款类型'],
            ['require','require','require','require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        $data        = [
            'fk_bdl_bd_customer'    => $this->customer_id,
            'fk_bdl_payvoucher'  => $this->params['fk_bdl_payvoucher'],
            'fk_bdl_amount'      => $this->params['fk_bdl_amount'],
            'fk_bdl_paytime'        => date('Y-m-d H:i:s',time()),
            'fk_bdl_paystatus'      => 1,
            'fk_bdl_paytype'        => $this->params['fk_bdl_paytype']
        ];
        $res         = \app\admin\model\Payrecord::where('id',$this->params['id'])->update($data);

        if($res)
        {
            $cacheTag       = \config('cache_tag')['v1_payrecord_listPayrecord'];
            Cache::clear($cacheTag);

            $this->success('修改成功,请等待审核');
        }

        $this->error('修改失败');
    }

}